package com.jessechanrui.template.skeleton.infrastructure.demo.support.repository;

import com.jessechanrui.template.skeleton.domain.demo.entity.Demo;
import com.jessechanrui.template.skeleton.domain.demo.support.repository.DemoRepository;
import com.jessechanrui.template.skeleton.infrastructure.demo.converter.DemoConverter;
import com.jessechanrui.template.skeleton.infrastructure.demo.dao.DemoDao;
import com.jessechanrui.template.skeleton.infrastructure.demo.po.DemoPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 示例应用服务
 * @author chenrui
 * @date 2024/1/30
 */
@Repository
public class DemoRepositoryImpl implements DemoRepository {
    @Autowired
    private DemoConverter demoConverter;

    @Autowired
    private DemoDao demoDao;

    @Override
    public Demo save(Demo demo) {
        DemoPO demoPO = demoConverter.toPO(demo);
        demoPO = demoDao.save(demoPO);
        demo = demoConverter.toEntity(demoPO);
        return demo;
    }

    @Override
    public Demo getById(int id) {
        Optional<DemoPO> poOpt = demoDao.findById(id);
        DemoPO demoPO = poOpt.get();
        Demo demo = demoConverter.toEntity(demoPO);
        return demo;
    }

    @Override
    public List<Demo> getAll() {
        List<DemoPO> all = demoDao.findAll();
        List<Demo> demos = demoConverter.toEntityList(all);
        return demos;
    }
}
